﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    namespace Boss1
    {
        public class StateTurret : State
        {
            public Alien turret;
            public Vector2 offset;

            StateShootStarPattern patternEasy;
            StateShootStarPattern patternMedium;
            StateShoot patternRandomShoot;

            StateSequence seqWaitShoot;
            StateShootSingle shootSingle;


            bool firstrun = true;

            public StateTurret(Alien a, Alien turret)
                : base(a)
            {
                this.turret = turret;
                turret.lifePoint = 4000.0f;

                patternEasy = new StateShootStarPattern(turret);
                patternEasy.fov = 18 * 2;
                patternEasy.delayUntilNextPattern = 1.0f;
                patternEasy.angleRotationIncrement = 0;
                patternEasy.waveCount = 6;

                patternMedium = new StateShootStarPattern(turret);
                patternMedium.fov = 18 * 4;
                patternMedium.angleRotationIncrement = 16;
                patternMedium.waveCount = 6;

                patternRandomShoot = new StateShoot(turret);
                patternRandomShoot.delayMin = 0.2f;
                patternRandomShoot.delayMax = 0.2f;
                patternRandomShoot.dirMin = new Vector2(-32, -32);
                patternRandomShoot.dirMax = new Vector2(+32, +32);

                {
                    StateWait wait = new StateWait(turret, 0.5f);
                    shootSingle = new StateShootSingle(turret);
                    shootSingle.count = 5;
                    shootSingle.fov = 90.0f;
                    seqWaitShoot = new StateSequence(turret);
                    seqWaitShoot.states.Add(wait);
                    seqWaitShoot.states.Add(shootSingle);
                    seqWaitShoot.loop = true;
                }
            }

            public override bool Update(Timer t)
            {
                if (firstrun)
                {
                    firstrun = false;
                    Game1.instance.alienManager.aliens.Add(turret);
                }

                turret.pos = alien.pos + offset;

                if (turret.lifePoint > 2000.0f)
                {
                    //patternEasy.Update(t);
                    shootSingle.target = Game1.instance.plane.pos;
                    seqWaitShoot.Update(t);
                }
                else
                { 
                    patternMedium.Update(t);
                    patternRandomShoot.Update(t);
                }

                return turret.IsAlive();
            }
        }

        public class StateSinMove : State
        {
            public Vector2 pos;
            public float range;
            public float speed;
            public float time;

            public StateSinMove(Alien a)
                : base(a)
            {

            }

            public override bool Update(Timer t)
            {
                time += t.Delta;
                alien.pos.X = Game1.width * 0.5f + (float)Math.Sin(time * speed) * range;
                return true;
            }
        }

        public class StateBoss : State
        {
            StateSequence mainSequence;
            StateSequence spawnSequence;
            Alien turret;

            public StateBoss(Alien a)
                : base(a)
            {
//                 Vector2[] offsets = new Vector2[] 
//                 { 
//                     new Vector2(0,0),
//                     new Vector2(0,0),
//                     new Vector2(-200,0),
//                     new Vector2(-200,0),
//                     new Vector2(+200,0),
//                     new Vector2(+200,0),
//                 };

                turret = new Alien();
                turret.colorIndex = 1;
                turret.pos = new Vector2(0,0);
                turret.speed = 0;
                turret.EventDie += new Alien.DieDelegate(turret_EventDie);

                turret.stateMachine.state = new StateTurret(a, turret);

                float px = Game1.width * 0.5f;
                StateGoto gotoMiddleOfTheScreen = new StateGoto(a);
                gotoMiddleOfTheScreen.dest = new Vector2(px, 128);
                gotoMiddleOfTheScreen.speed = 200;
                gotoMiddleOfTheScreen.slowDownDistance = 100;

                Boss1.StateSinMove sinMove = new Boss1.StateSinMove(a);
                sinMove.speed = 1;
                sinMove.range = Game1.width * 0.1f;

                StateTurret spawnTurret0 = new StateTurret(alien, turret);
                spawnSequence = new StateSequence(a, new State[] { spawnTurret0 });

                mainSequence = new StateSequence(a);
                mainSequence.states.Add(gotoMiddleOfTheScreen);
                mainSequence.states.Add(new StateParallel(a, new State[] { sinMove, spawnSequence }));
            }

            void turret_EventDie(Alien a)
            {

            }

            public override bool Update(Timer t)
            {
                mainSequence.Update(t);
                return true;
            }
        }
    }

    public class Level1Boss
    {
        static public void Setup(AlienManager alienManager)
        {
            Alien boss = new Alien();
            alienManager.aliens.Add(boss);

            float px = Game1.width / 2;

            boss.pos = new Vector2(px, -32);
            boss.size = new Vector2(512, 128);
            boss.hitBox = new Vector2(-1, -1);            
            boss.lifePoint = 1000;
            boss.collision = false;
            boss.stateMachine.state = new Boss1.StateBoss(boss);
        }
    }

}
